import { getFilePage, DeletFile } from '@/api/filepage'
import { Message } from 'element-ui'
const state = {
  fileList: [],
  fileCount: 0
}

const mutations = {
  update(state, payload) {
    for (const key in payload) {
      state[key] = payload[key]
    }
  }
}

const actions = {
  async getFilePage({ commit }, payload = {}) {
    console.log(payload)
    const { page = 1, pageSize = 12, ...options } = payload
    const result = await getFilePage(page, options, pageSize)
    console.log('result...', result)
    if (result.data) {
      commit('update', {
        fileCount: result.data[1],
        fileList: result.data[0]
      })
    }
  },
  async DeletFile({ commit }, payload) {
    const { id, page, pageSize } = payload
    const options = {}
    console.log(id, page)
    const result = await DeletFile(id)
    if (result) {
      Message.success('操作成功')
      const result = await getFilePage(page, options, pageSize)
      if (result.data) {
        commit('update', {
          fileCount: result.data[1],
          fileList: result.data[0]
        })
      }
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
